public class Test11_21 {
    int queen[] = new int[8];
    static int count = 0;
    public static void main(String args[]) {
        Test11_21 queen = new Test11_21();
        queen.eightQueen(0);
        System.out.println("总共有 " +count+ " 摆放方法");
    }
    //判断是否合法
    boolean isLegal(int currentRow,int currentColumn) {
        for(int preColumn=0;preColumn<currentColumn;preColumn++) {
            int row = queen[preColumn];
            if(row==currentRow) {
                return false;
            }
            int rowDiff= Math.abs(row - currentRow);
            int columnDiff = Math.abs(currentColumn-preColumn);
            if(rowDiff==columnDiff){
                return false;
            }
        }
        return true;
    }
    private void eightQueen(int currentColumn) {
        for(int row=0;row<8;row++) {
            if(isLegal(row,currentColumn)) {
                queen[currentColumn] = row;
                if(currentColumn!=7) {
                    eightQueen(currentColumn+1);
                }else {
                    count++;
                }
            }
        }
    }

}
